﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ChineseSchool.Models.Hethong;
using ChineseSchool.BLL;
using System.Data;

namespace ChineseSchool.Admin
{
    public partial class QL_Page : System.Web.UI.Page
    {
        int MenuTypeID;
        B_Page pageServices = new B_Page();
        B_Menu MenuServices = new B_Menu();
        B_MenuType MenuTypeServices = new B_MenuType();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                BinGrid();
                btnAdd.Visible = true;
                btnEdit.Visible = true;
            }
        }

        protected void BinGrid()
        {
            DataTable lstM_Page = pageServices.GetAll();
            grdPage.DataSource = lstM_Page;
            grdPage.DataBind();
        }


        protected void BindMenuData(int MenuTypeID)
        {
            List<M_Menu> lst_Menu = (new B_Menu()).GetBy_MenuTypeID(MenuTypeID);
            ddlMenu.DataSource = lst_Menu;
            ddlMenu.DataValueField = "MenuID";
            ddlMenu.DataTextField = "MenuName";
            ddlMenu.DataBind();
        }

        protected void BindMenuType()
        {
            DataTable dt_Menu = (new B_MenuType()).GetAll();
            ddlMenutype.DataSource = dt_Menu;
            ddlMenutype.DataValueField = "MenuTypeID";
            ddlMenutype.DataTextField = "MenuTypeName";
            ddlMenutype.DataBind();
        }

        protected void grdPage_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grdPage.PageIndex = e.NewPageIndex;
            BinGrid();
        }

        protected void grdPage_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            int PageID = Convert.ToInt32(e.CommandArgument);
            M_Page M_Page = new M_Page();
            B_Page B_Page = new B_Page();
            B_Insert_Update_Delete B_Insert_Update_Delete = new B_Insert_Update_Delete();
            if (e.CommandName.Equals("Delete", StringComparison.OrdinalIgnoreCase))
            {
                try
                {
                    B_Insert_Update_Delete.delete("Page", "PageID", M_Page, PageID);
                    Response.Write("<script>alert('Đã được xóa!')</script>");
                }
                catch (System.Exception ex)
                {
                    throw new Exception("Đã có lỗi xảy ra! Vui lòng thử lại lần sau!", ex);
                }
                BinGrid();
            }
            if (e.CommandName.Equals("Edit", StringComparison.OrdinalIgnoreCase))
            {
                panAdd.Visible = true;
                btnAdd.Visible = false;
                lblTitle.Text = "Cập nhật";

                M_Page = B_Page.Selectby_id(PageID);
                BindMenuType();

                if (M_Page.menuID > 0)
                {
                    int menuType = MenuServices.GetByPK(M_Page.menuID).menuTypeID;
                    ddlMenutype.ClearSelection();
                    ddlMenutype.Items.FindByValue(menuType.ToString()).Selected = true;

                    BindMenuData(menuType);
                    ddlMenu.ClearSelection();
                    ddlMenu.Items.FindByValue(M_Page.menuID.ToString()).Selected = true;
                }                

                txtTitle.Text = M_Page.title;
                ckPageContent.Value = M_Page.pageContent;

                btnEdit.CommandArgument = PageID.ToString();
            }
        }

        protected void btnNew_Click(object sender, EventArgs e)
        {
            panAdd.Visible = true;
            btnEdit.Visible = false;
            lblTitle.Text = "Thêm mới";
            txtTitle.Text = "";
            ckPageContent.Value = "";
            BindMenuType();
            BindMenuData(MenuTypeID);
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            B_Insert_Update_Delete B_Insert_Update_Delete = new B_Insert_Update_Delete();
            panAdd.Visible = false;
            btnEdit.Visible = true;
            M_Page M_Page = new M_Page();
            M_Page.title = txtTitle.Text;
            int MenuID = 0;
            if (ddlMenu.SelectedValue.ToString() != "" && ddlMenu.SelectedValue.ToString() != null)
            {
                MenuID = Convert.ToInt32(ddlMenu.SelectedValue.ToString());
            }
            M_Page.menuID = MenuID;
            M_Page.pageContent = ckPageContent.Value;
            B_Insert_Update_Delete.insert("Page", "PageID", M_Page);
            BinGrid();
        }

        protected void btnEdit_Click(object sender, EventArgs e)
        {
            B_Insert_Update_Delete B_Insert_Update_Delete = new B_Insert_Update_Delete();
            panAdd.Visible = false;
            btnAdd.Visible = true;
            M_Page M_Page = new M_Page();
            B_Page B_Page = new B_Page();
            int PageID = Convert.ToInt32(btnEdit.CommandArgument);
            M_Page.pageID = Convert.ToInt32(btnEdit.CommandArgument);
            M_Page.title = txtTitle.Text;
            int MenuID = 0;
            if (ddlMenu.SelectedValue.ToString() != "" && ddlMenu.SelectedValue.ToString() != null)
            {
                MenuID = Convert.ToInt32(ddlMenu.SelectedValue.ToString());
            }
            M_Page.menuID = MenuID;
            M_Page.pageContent = ckPageContent.Value;

            B_Insert_Update_Delete.update("Page", "PageID", M_Page, PageID);
            BinGrid();
        }

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            panAdd.Visible = false;
            btnAdd.Visible = true;
            btnEdit.Visible = true;
            txtTitle.Text = "";
            ckPageContent.Value = "";
        }

        protected void grdPage_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            BinGrid();
        }

        protected void grdPage_RowEditing(object sender, GridViewEditEventArgs e)
        {
            grdPage.EditIndex = e.NewEditIndex;
            BinGrid();
        }

        protected void ddlMenutype_SelectedIndexChanged(object sender, EventArgs e)
        {
            MenuTypeID = Convert.ToInt32(ddlMenutype.SelectedValue.ToString());
            BindMenuData(MenuTypeID);
        }
    }
}